﻿Option Strict Off
Option Explicit On

Imports Microsoft.VisualBasic.Compatibility

Public Class SRM
    Private Const c_Tipo As String = "08"
    Private Const c_Contexto As String = "00"

#Region " Variables Privadas "
    Private Declare Function Srmw32 Lib "c:\bin\f\Srmw32.dll" (ByVal Nodo As String, ByVal Apli As String, ByVal msg As String, ByRef Largo As Integer, ByVal Status As String, ByVal funcion As String, ByVal Contexto As String, ByVal Control As String) As Short

    Private m_RetornoAplicacion As String
    Private m_RetornoSrm As Integer
    Private m_StatusSrm As Short
    Private m_Respuesta As String

    Private m_Mensaje As String
    Private m_Aplicacion As String
    Private m_Nodo As String
    Private m_Tipo As String
    Private m_Contexto As String
    Private m_CodigoServicio As Integer
#End Region

#Region " Métodos y Funciones "
    Public Function Invocar(ByVal Aplicacion As String, ByVal Nodo As String, ByVal Mensaje As String) As Boolean
        m_Aplicacion = Aplicacion
        m_Nodo = Nodo
        m_Mensaje = Mensaje

        Return Invocar()
    End Function

    Public Function Invocar(ByVal Mensaje As String) As Boolean
        m_Mensaje = Mensaje

        Return Invocar()
    End Function

    Public Function Clonar() As SRM
        Dim newSRMInfo As SRM = New SRM

        With newSRMInfo
            .CodigoServicio = m_CodigoServicio
            .Nodo = m_Nodo
            .Aplicacion = m_Aplicacion
            .Tipo = m_Tipo
            .Contexto = m_Contexto
            '.SleepNro = m_SleepNro
            '.SleepTime = m_SleepTime
        End With

        Return newSRMInfo
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Invocar() As Boolean
        Dim Inicio As Date
        Dim strResponse As New VB6.FixedLengthString(8166)
        Dim strStatus As String
        Dim strControl As String ' Control.
        Dim lngLength As Integer ' Largo del Mensaje.
        Dim Retorno As Boolean
        Dim Ahora As Date

        Try
            If m_Mensaje = "" Then
                Throw New SRMFaltaParamException("Falta parámetro Mensaje")
            End If

            If m_Nodo = "" Then
                Throw New SRMFaltaParamException("Falta parámetro Nodo")
            End If

            If m_Aplicacion = "" Then
                Throw New SRMFaltaParamException("Falta parámetro Aplicación")
            End If

            m_Tipo = IIf(m_Tipo = "", c_Tipo, m_Tipo)
            m_Contexto = IIf(m_Contexto = "", c_Contexto, m_Contexto)

            strStatus = "00"
            strControl = Space(10000)

            strResponse.Value = m_Mensaje & Chr(0)
            lngLength = Len(m_Mensaje)

            Inicio = Now
            m_RetornoSrm = Srmw32(m_Nodo, m_Aplicacion, strResponse.Value, lngLength, strStatus, IIf(m_Tipo = "", c_Tipo, m_Tipo), IIf(m_Contexto = "", c_Contexto, m_Contexto), strControl)
            Ahora = Now

            m_Respuesta = Mid(strResponse.Value, 1, lngLength)
            m_StatusSrm = Val(strStatus)

            If lngLength >= 2 Then
                m_RetornoAplicacion = Mid(m_Respuesta, 1, 2)
            End If
        Catch exSRM As SRMFaltaParamException
            m_Respuesta = exSRM.Message
            Retorno = False
        Finally
            If m_RetornoSrm = 0 Then
                If IsNumeric(m_RetornoAplicacion) Then
                    If Val(m_RetornoAplicacion) = 0 Then
                        Retorno = True
                    Else
                        Retorno = False
                    End If
                Else
                    Retorno = False
                End If
            Else
                Retorno = False
            End If
        End Try

        Return Retorno
    End Function

    Public Function WriteToEventLog(ByVal Entry As String, Optional ByVal AppName As String = "APlusFeeCalc", Optional ByVal EventType As EventLogEntryType = EventLogEntryType.Information, Optional ByVal LogName As String = "Application") As Boolean
        Dim objEventLog As New EventLog

        Try
            If Not EventLog.SourceExists(AppName) Then
                EventLog.CreateEventSource(AppName, LogName)
            End If

            objEventLog.Source = AppName
            objEventLog.WriteEntry(Entry, EventType)

            Return True
        Catch Ex As Exception
            Return False
        End Try
    End Function


#End Region

#Region " Propiedades "
    Public Property CodigoServicio() As Integer
        Get
            Return m_CodigoServicio
        End Get
        Set(ByVal Value As Integer)
            m_CodigoServicio = Value
        End Set
    End Property

    Public Property Nodo() As String
        Get
            Nodo = m_Nodo
        End Get
        Set(ByVal Value As String)
            m_Nodo = Trim(Value)
        End Set
    End Property

    Public Property Aplicacion() As String
        Get
            Aplicacion = m_Aplicacion
        End Get
        Set(ByVal Value As String)
            m_Aplicacion = Trim(Value)
        End Set
    End Property

    Public Property Mensaje() As String
        Get
            Mensaje = m_Mensaje
        End Get
        Set(ByVal Value As String)
            m_Mensaje = Trim(Value)
        End Set
    End Property

    Public Property Tipo() As String
        Get
            Tipo = m_Tipo
        End Get
        Set(ByVal Value As String)
            m_Tipo = Trim(Value)
        End Set
    End Property

    Public Property Contexto() As String
        Get
            Return m_Contexto
        End Get
        Set(ByVal Value As String)
            m_Contexto = Trim(Value)
        End Set
    End Property

    'Definicion de propiedades de solo lectura
    Public ReadOnly Property Respuesta() As String
        Get
            Respuesta = m_Respuesta
        End Get
    End Property

    Public Property StatusSrm() As Short
        Get
            StatusSrm = m_StatusSrm
        End Get
        Set(ByVal value As Short)
            m_StatusSrm = value
        End Set
    End Property

    Public Property RetornoSrm() As Short
        Get
            RetornoSrm = m_RetornoSrm
        End Get
        Set(ByVal value As Short)
            m_RetornoSrm = value
        End Set
    End Property

    Public Property RetornoAplicacion() As String
        Get
            Return m_RetornoAplicacion
        End Get
        Set(ByVal value As String)
            m_RetornoAplicacion = value
        End Set
    End Property

    Public Property RetornoApp() As String
        Get
            Return m_RetornoAplicacion
        End Get
        Set(ByVal value As String)
            m_RetornoAplicacion = value
        End Set
    End Property
#End Region

#Region " Constructor "
    Public Sub New()
        m_Aplicacion = ""
        m_Nodo = ""
        m_Tipo = c_Tipo
        m_Contexto = c_Contexto
    End Sub

    Public Sub New(ByVal Aplicacion As String, ByVal Nodo As String, Optional ByVal Tipo As String = "08", Optional ByVal Contexto As String = "00")
        MyBase.New()

        m_Aplicacion = Aplicacion
        m_Nodo = Nodo
        m_Tipo = Tipo
        m_Contexto = Contexto
    End Sub
#End Region

End Class
